﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace Jy123.Web.Manage
{
    public partial class PropsType : MgConfig
    {
        private Dictionary<int, Jy123.Model.Public_PropsType> grandDic = new Dictionary<int, Jy123.Model.Public_PropsType>();
        private Dictionary<int, Jy123.Model.Public_PropsType> fatherDic = new Dictionary<int, Jy123.Model.Public_PropsType>();
        private List<Jy123.Model.Public_PropsType> listPropsType;
        private List<Jy123.Model.Public_PropsType> listFatherPropsType;
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("PropsType.aspx");
            Jy123.BLL.Public_PropsType BLLPropsType = new Jy123.BLL.Public_PropsType();
            listPropsType = BLLPropsType.GetModelList("PFather IS NULL");
            listFatherPropsType = BLLPropsType.GetModelList("PFather IS NOT NULL");
            foreach (Jy123.Model.Public_PropsType type in listPropsType)
            {
                grandDic.Add(type.ID, type);
            }
            foreach (Jy123.Model.Public_PropsType fType in listFatherPropsType)
            {
                fatherDic.Add(fType.ID, fType);
            }
            if (!Page.IsPostBack)
            {
                List<Jy123.Model.Public_PropsType> listAllPropsType = BLLPropsType.GetModelList(string.Empty);
                LoadInitialValue(listAllPropsType);
            }
        }

        protected void Repeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.Public_PropsType propsType = e.Item.DataItem as Jy123.Model.Public_PropsType;
                HiddenField idHidden = e.Item.FindControl("idHidden") as HiddenField;
                idHidden.Value = propsType.ID.ToString();
                Label nameLabel = e.Item.FindControl("propsTypeNameLabel") as Label;
                nameLabel.Text = propsType.PName;
                Label fatherNameLabel = e.Item.FindControl("fatherNameLabel") as Label;
                if (propsType.PFather.HasValue)
                {
                    string root = propsType.PTree.Substring(0, 3);
                    if (root.Contains(propsType.PFather.Value.ToString()))
                    {
                        fatherNameLabel.Text = grandDic[propsType.PFather.Value].PName;
                    }
                    else
                    {
                        fatherNameLabel.Text = fatherDic[propsType.PFather.Value].PName;
                    }

                }
            }
        }

        protected void Repeater_Command(object sender, RepeaterCommandEventArgs e)
        {
            RepeaterItem item = e.Item as RepeaterItem;
            HiddenField hidden = e.Item.FindControl("idHidden") as HiddenField;
            int id = int.Parse(hidden.Value);
            Jy123.BLL.Public_PropsType BLLPropsType = new Jy123.BLL.Public_PropsType();
            if (e.CommandName.Equals("Edit"))
            {
                Jy123.Model.Public_PropsType propsType = BLLPropsType.GetModel(id);
                actionTypeHidden.Value = "Update";
                idHidden.Value = id.ToString();
                propsTypeNameTextBox.Text = propsType.PName;
                if (propsType.PFather.HasValue)
                {
                    Model.Public_PropsType fatherType = BLLPropsType.GetModel(propsType.PFather.Value);
                    if (fatherType.PFather.HasValue)
                    {
                        grandDropDown.SelectedValue = fatherType.PFather.Value.ToString();
                        Grand_Change(grandDropDown, EventArgs.Empty);
                        fatherDropDown.SelectedValue = propsType.PFather.Value.ToString();
                    }
                    else
                    {
                        grandDropDown.SelectedValue = propsType.PFather.Value.ToString();
                    }
                }
                else
                {
                    List<Jy123.Model.Public_PropsType> listCPropsType = new List<Jy123.Model.Public_PropsType>();
                    foreach (Jy123.Model.Public_PropsType cType in listPropsType)
                    {
                        if (cType.ID != propsType.ID)
                        {
                            listCPropsType.Add(cType);
                        }
                    }
                    BindDropDown(listCPropsType, grandDropDown);
                    Grand_Change(grandDropDown, EventArgs.Empty);
                }
            }
            else if (e.CommandName.Equals("Delete"))
            {
                BLLPropsType.Delete(id);
                List<Jy123.Model.Public_PropsType> listAllPropsType = BLLPropsType.GetModelList(string.Empty);
                LoadInitialValue(listAllPropsType);
                Grand_Change(grandDropDown, EventArgs.Empty);
                fatherDropDown.SelectedValue = "-1";
                ClearPage();
            }
        }

        protected void Save(object sender, EventArgs e)
        {
            Jy123.BLL.Public_PropsType BLLPropsType = new Jy123.BLL.Public_PropsType();
            if (actionTypeHidden.Value.Equals("Add"))
            {
                Jy123.Model.Public_PropsType propsType = new Jy123.Model.Public_PropsType();
                propsType.ID = BLLPropsType.Add(propsType);//为PTree字段，此处先创建一条空记录，再更新此记录
                SetProsTypeValue(propsType);
                BLLPropsType.Update(propsType);
            }
            else
            {
                int id = int.Parse(idHidden.Value);
                Jy123.Model.Public_PropsType propsType = BLLPropsType.GetModel(id);
                SetProsTypeValue(propsType);
                BLLPropsType.Update(propsType);
            }
            List<Jy123.Model.Public_PropsType> listAllPropsType = BLLPropsType.GetModelList(string.Empty);
            LoadInitialValue(listAllPropsType);
            Grand_Change(grandDropDown, EventArgs.Empty);
            fatherDropDown.SelectedValue = "-1";
            ClearPage();
        }

        private void ClearPage()
        {
            actionTypeHidden.Value = "Add";
            propsTypeNameTextBox.Text = string.Empty;
        }

        private void SetProsTypeValue(Jy123.Model.Public_PropsType type)
        {
            type.PName = propsTypeNameTextBox.Text;
            int fatherId = int.Parse(fatherDropDown.SelectedValue);
            if (fatherId > 0)
            {
                type.PFather = fatherId;
                Jy123.Model.Public_PropsType fatherType = fatherDic[fatherId];
                type.PTree = fatherType.PTree + type.ID.ToString() + ",";
            }
            else
            {
                int grandId = int.Parse(grandDropDown.SelectedValue);
                if (grandId > 0)
                {
                    Jy123.Model.Public_PropsType grandType = grandDic[grandId];
                    type.PFather = grandId;
                    type.PTree = grandType.PTree + type.ID.ToString() + ",";
                }
                else
                {
                    type.PFather = null;
                    type.PTree = "," + type.ID.ToString() + ",";
                }

            }
        }

        private void LoadInitialValue(List<Jy123.Model.Public_PropsType> cListPropsType)
        {
            propsTypeRepeater.DataSource = cListPropsType;
            propsTypeRepeater.DataBind();
            propsTypeRepeater.Visible = cListPropsType.Count > 0;
            BindDropDown(listPropsType, grandDropDown);
        }

        private void BindDropDown(List<Jy123.Model.Public_PropsType> listPropsType, DropDownList dropDown)
        {
            dropDown.Items.Clear();
            dropDown.DataSource = listPropsType;
            dropDown.DataTextField = "PName";
            dropDown.DataValueField = "ID";
            dropDown.DataBind();
            dropDown.Items.Insert(0, new ListItem("请选择", "-1"));
        }

        protected void Grand_Change(object sender, EventArgs e)
        {
            int id = int.Parse(grandDropDown.SelectedValue);

            BLL.Public_PropsType BLLPType = new Jy123.BLL.Public_PropsType();
            string strWhere = string.Format("PFather = {0}", id.ToString());
            List<Jy123.Model.Public_PropsType> listPropsType = BLLPType.GetModelList(strWhere);
            BindDropDown(listPropsType, fatherDropDown);
            fatherDropDown.Visible = listPropsType.Count > 0;
        }
    }
}

